package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.GsonBasketball;
import com.lanou.sinasports.beans.GsonFootball;
import com.lanou.sinasports.utils.DoubleCache;
import com.lanou.sinasports.utils.GsonRequest;

/**
 * Created by dllo on 16/1/11.
 */
public class SignRankFragment extends Fragment {
    private TextView tvFootOrderOne;
    private NetworkImageView ivFootPicOne;
    private TextView  tvFootNameOne;
    private TextView  tvFootCountOne;

    private TextView tvFootOrderTwo;
    private NetworkImageView ivFootPicTwo;
    private TextView  tvFootNameTwo;
    private TextView  tvFootCountTwo;

    private TextView tvFootOrderThree;
    private NetworkImageView ivFootPicThree;
    private TextView  tvFootNameThree;
    private TextView  tvFootCountThree;

    private TextView tvFootOrderFour;
    private NetworkImageView ivFootPicFour;
    private TextView  tvFootNameFour;
    private TextView  tvFootCountFour;


    private TextView tvBasOrderOne;
    private NetworkImageView ivBasPicOne;
    private TextView  tvBasNameOne;
    private TextView  tvBasCountOne;

    private TextView tvBasOrderTwo;
    private NetworkImageView ivBasPicTwo;
    private TextView  tvBasNameTwo;
    private TextView  tvBasCountTwo;

    private TextView  tvBasOrderThree;
    private NetworkImageView ivBasPicThree;
    private TextView  tvBasNameThree;
    private TextView  tvBasCountThree;

    private TextView tvBasOrderFour;
    private NetworkImageView ivBasPicFour;
    private TextView  tvBasNameFour;
    private TextView  tvBasCountFour;

    private Context context;

    private String url1="http://platform.sina.com.cn/sports_client/team_hot?app_key=2586208540&type=1&num=4";
    private String url2="http://platform.sina.com.cn/sports_client/team_hot?app_key=2586208540&type=2&num=4";

    private RequestQueue queue;
    private ImageLoader imageLoader;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_sign_rank,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvFootOrderOne= (TextView) view.findViewById(R.id.tv_rank_football_order_one);
        ivFootPicOne= (NetworkImageView) view.findViewById(R.id.iv_rank_football_pic_one);
        tvFootNameOne= (TextView) view.findViewById(R.id.tv_rank_football_name_one);
        tvFootCountOne= (TextView) view.findViewById(R.id.tv_rank_football_count_one);

        tvFootOrderTwo= (TextView) view.findViewById(R.id.tv_rank_football_order_two);
        ivFootPicTwo= (NetworkImageView) view.findViewById(R.id.iv_rank_football_pic_two);
        tvFootNameTwo= (TextView) view.findViewById(R.id.tv_rank_football_name_two);
        tvFootCountTwo= (TextView) view.findViewById(R.id.tv_rank_football_count_two);

        tvFootOrderThree= (TextView) view.findViewById(R.id.tv_rank_football_order_three);
        ivFootPicThree= (NetworkImageView) view.findViewById(R.id.iv_rank_football_pic_three);
        tvFootNameThree= (TextView) view.findViewById(R.id.tv_rank_football_name_three);
        tvFootCountThree= (TextView) view.findViewById(R.id.tv_rank_football_count_three);

        tvFootOrderFour= (TextView) view.findViewById(R.id.tv_rank_football_order_four);
        ivFootPicFour= (NetworkImageView) view.findViewById(R.id.iv_rank_football_pic_four);
        tvFootNameFour= (TextView) view.findViewById(R.id.tv_rank_football_name_four);
        tvFootCountFour= (TextView) view.findViewById(R.id.tv_rank_football_count_four);




        tvBasOrderOne= (TextView) view.findViewById(R.id.tv_rank_basketball_order_one);
        ivBasPicOne= (NetworkImageView) view.findViewById(R.id.iv_rank_basketball_pic_one);
        tvBasNameOne= (TextView) view.findViewById(R.id.tv_rank_basketball_name_one);
        tvBasCountOne= (TextView) view.findViewById(R.id.tv_rank_basketball_count_one);

        tvBasOrderTwo= (TextView) view.findViewById(R.id.tv_rank_basketball_order_two);
        ivBasPicTwo= (NetworkImageView) view.findViewById(R.id.iv_rank_basketball_pic_two);
        tvBasNameTwo= (TextView) view.findViewById(R.id.tv_rank_basketball_name_two);
        tvBasCountTwo= (TextView) view.findViewById(R.id.tv_rank_basketball_count_two);

        tvBasOrderThree= (TextView) view.findViewById(R.id.tv_rank_basketball_order_three);
        ivBasPicThree= (NetworkImageView) view.findViewById(R.id.iv_rank_basketball_pic_three);
        tvBasNameThree= (TextView) view.findViewById(R.id.tv_rank_basketball_name_three);
        tvBasCountThree= (TextView) view.findViewById(R.id.tv_rank_basketball_count_three);

        tvBasOrderFour= (TextView) view.findViewById(R.id.tv_rank_basketball_order_four);
        ivBasPicFour= (NetworkImageView) view.findViewById(R.id.iv_rank_basketball_pic_four);
        tvBasNameFour= (TextView) view.findViewById(R.id.tv_rank_basketball_name_four);
        tvBasCountFour= (TextView) view.findViewById(R.id.tv_rank_basketball_count_four);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        queue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(queue,new DoubleCache());
        GsonRequest<GsonFootball> requestFoot=new GsonRequest<GsonFootball>(url1, GsonFootball.class, new Response.Listener<GsonFootball>() {
            @Override
            public void onResponse(GsonFootball gsonFootball) {
                int size=gsonFootball.getResult().getData().size();
                for(int i=0;i<size;i++){
                    if(i==0){
                        tvFootOrderOne.setText(gsonFootball.getResult().getData().get(i).getRank());
                        tvFootNameOne.setText(gsonFootball.getResult().getData().get(i).getTname_cn());
                        tvFootCountOne.setText(gsonFootball.getResult().getData().get(i).getCount());
                        ivFootPicOne.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivFootPicOne.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivFootPicOne.setImageUrl(gsonFootball.getResult().getData().get(i).getLogo(),imageLoader);
                    }
                    if(i==1){
                        tvFootOrderTwo.setText(gsonFootball.getResult().getData().get(i).getRank());
                        tvFootNameTwo.setText(gsonFootball.getResult().getData().get(i).getTname_cn());
                        tvFootCountTwo.setText(gsonFootball.getResult().getData().get(i).getCount());
                        ivFootPicTwo.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivFootPicTwo.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivFootPicTwo.setImageUrl(gsonFootball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                    if(i==2){
                        tvFootOrderThree.setText(gsonFootball.getResult().getData().get(i).getRank());
                        tvFootNameThree.setText(gsonFootball.getResult().getData().get(i).getTname_cn());
                        tvFootCountThree.setText(gsonFootball.getResult().getData().get(i).getCount());
                        ivFootPicThree.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivFootPicThree.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivFootPicThree.setImageUrl(gsonFootball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                    if(i==3){
                        tvFootOrderFour.setText(gsonFootball.getResult().getData().get(i).getRank());
                        tvFootNameFour.setText(gsonFootball.getResult().getData().get(i).getTname_cn());
                        tvFootCountFour.setText(gsonFootball.getResult().getData().get(i).getCount());
                        ivFootPicFour.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivFootPicFour.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivFootPicFour.setImageUrl(gsonFootball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue.add(requestFoot);


        GsonRequest<GsonBasketball> requestBas=new GsonRequest<GsonBasketball>(url2, GsonBasketball.class, new Response.Listener<GsonBasketball>() {
            @Override
            public void onResponse(GsonBasketball gsonBasketball) {
                int size=gsonBasketball.getResult().getData().size();
                for(int i=0;i<size;i++){
                    if(i==0){
                        tvBasOrderOne.setText(gsonBasketball.getResult().getData().get(i).getRank());
                        tvBasNameOne.setText(gsonBasketball.getResult().getData().get(i).getTname_cn());
                        tvBasCountOne.setText(gsonBasketball.getResult().getData().get(i).getCount());
                        ivBasPicOne.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivBasPicOne.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivBasPicOne.setImageUrl(gsonBasketball.getResult().getData().get(i).getLogo(),imageLoader);
                    }
                    if(i==1){
                        tvBasOrderTwo.setText(gsonBasketball.getResult().getData().get(i).getRank());
                        tvBasNameTwo.setText(gsonBasketball.getResult().getData().get(i).getTname_cn());
                        tvBasCountTwo.setText(gsonBasketball.getResult().getData().get(i).getCount());
                        ivBasPicTwo.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivBasPicTwo.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivBasPicTwo.setImageUrl(gsonBasketball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                    if(i==2){
                        tvBasOrderThree.setText(gsonBasketball.getResult().getData().get(i).getRank());
                        tvBasNameThree.setText(gsonBasketball.getResult().getData().get(i).getTname_cn());
                        tvBasCountThree.setText(gsonBasketball.getResult().getData().get(i).getCount());
                        ivBasPicThree.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivBasPicThree.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivBasPicThree.setImageUrl(gsonBasketball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                    if(i==3){
                        tvBasOrderFour.setText(gsonBasketball.getResult().getData().get(i).getRank());
                        tvBasNameFour.setText(gsonBasketball.getResult().getData().get(i).getTname_cn());
                        tvBasCountFour.setText(gsonBasketball.getResult().getData().get(i).getCount());
                        ivBasPicFour.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                        ivBasPicFour.setDefaultImageResId(R.mipmap.ic_main_tab_special_p);
                        ivBasPicFour.setImageUrl(gsonBasketball.getResult().getData().get(i).getLogo(), imageLoader);
                    }
                }



            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });
        queue.add(requestBas);

    }
}
